//
//  ComprehensiveView.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ComprehensiveView.h"
#import "QuartzCore/QuartzCore.h"
#import "ComprehensiveTableCell.h"
#import "ZHWebService.h"
#import "ZHDataService.h"
#import "ActivityViewManager.h"
#import "UIImageView+LoadImagesFromInternet.h"
#import "PersonalViewController.h"
#import "ChangeInfoViewController.h"
#import "LocalUser.h"

NSString *kUserLoginedNotification = @"kUserLoginedNotification";

@implementation ComprehensiveView

@synthesize userImageView,userNameLabel;

#pragma mark - my own methods
- (void)getLoginUserDataWithUsername:(NSString *)name andPassword:(NSString *)pwd autoLogin:(BOOL)isAuto
{
    //get login user info from internet
    CompletionBlock comBlock = ^(NSData *data){
        BOOL succeed = [ZHDataService parseToLoginUserWithData:data];
        if(succeed)
        {
            //save user to local user
            LocalUser *lu = [[LocalUser alloc] init];
            lu.name = name;
            lu.pwd = pwd;
            lu.isAuto = isAuto;
            lu.loginDate = [NSDate date];
            NSInteger index = 0;
            for(LocalUser *u in [ZHDataService sharedService].localUsers)
            {
                if([u.name isEqualToString:name])
                    break;
                index++;
            }
            if(index == [ZHDataService sharedService].localUsers.count)
                [[ZHDataService sharedService].localUsers addObject:lu];
            else
                [[ZHDataService sharedService].localUsers replaceObjectAtIndex:index withObject:lu];
            [lu release];
            [[ZHDataService sharedService].localUsers sortUsingComparator:^(LocalUser *u1, LocalUser *u2){
                   return [u1.loginDate compare:u2.loginDate];
            }];
            
            
            [[ZHDataService sharedService] loginUser].userPwd = pwd;
            self.userNameLabel.text = [[ZHDataService sharedService] loginUser].userName;
            [self.userImageView loadImageAsynchronousWithURL:[[ZHDataService sharedService] loginUser].userAvatarurl placeHolderImage:kDefaultUserImageName];
            [self.baseTableView reloadData];
            
            PersonalViewController *personalController = (PersonalViewController *)[[[self superview] superview] nextResponder];
            if([personalController.navigationController.tabBarController modalViewController])
            {
                [personalController performSelector:@selector(dismissModalViewControllerAnimated:) withObject:[NSNumber numberWithBool:YES] afterDelay:0.5];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:kUserLoginedNotification object:nil];
        }
        else
        {
            [ActivityViewManager showErrorViewWithMessage:@"用户名或密码错误！"];
        }
    };
    ErrorBlock errBlock = ^(NSString *error){};
    
    [[ZHWebService sharedService] getLoginUserInfoWithUsername:name password:pwd onCompletion:comBlock onError:errBlock];
}

#pragma mark - View lifecycle

- (void)dealloc
{
    [userNameLabel release];
    [userImageView release];
    [super dealloc];
}

- (void)setUserImageViewShowWithRoundCorner
{
    self.userImageView.layer.masksToBounds = YES;
    self.userImageView.layer.cornerRadius = 5.f;
    self.userImageView.layer.borderWidth = 1.f;
    self.userImageView.layer.borderColor = [[UIColor grayColor] CGColor];
}

- (void)awakeFromNib
{
    [self setUserImageViewShowWithRoundCorner];
    self.baseTableView.backgroundColor = [UIColor clearColor];
}

#pragma mark -
#pragma UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
        return 3;
    else if(section == 1)
        return 2;
    else
        return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cellIdentify";
    
    if(indexPath.section == 0)
    {
        ComprehensiveTableCell *cell = (ComprehensiveTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if(cell == nil)
        {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"ComprehensiveTableCell" owner:self options:nil];
            cell = (ComprehensiveTableCell *)[nibs lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        if(indexPath.row == 0)
        {
            cell.keyLabel.text = @"用户名";
            cell.valueLabel.text = [[ZHDataService sharedService] loginUser].userName;
        }
        else if(indexPath.row == 1)
        {
            cell.keyLabel.text = @"邮箱";
            cell.valueLabel.text = [[ZHDataService sharedService] loginUser].userEmail;
        }
        else
        {
            cell.keyLabel.text = @"积分";
            cell.valueLabel.text =[NSString stringWithFormat:@"%d",[[ZHDataService sharedService] loginUser].userCredits];
        }
        return cell;
    }
    else if(indexPath.section == 1)
    {
        ComprehensiveTableCell *cell = (ComprehensiveTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if(cell == nil)
        {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"ComprehensiveTableCell" owner:self options:nil];
            cell = (ComprehensiveTableCell *)[nibs lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        if(indexPath.row == 0)
        {
            cell.keyLabel.text = @"用户组";
            cell.valueLabel.text = [[ZHDataService sharedService] loginUser].userGrouptitle;
        }
        else
        {
            cell.keyLabel.text = @"注册于";
            cell.valueLabel.text = [[ZHDataService sharedService] loginUser].userRegdate;
        }
        return cell;
    }
    else {
        static NSString *cellIdentify2 = @"cellIdentify2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify2];
        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify2] autorelease];
            cell.textLabel.textColor = [UIColor cellLeftKeyTextColor];
            cell.textLabel.font = [UIFont getSettingViewTableCellLabelFont];
        }
        cell.textLabel.text = @"修改个人信息";
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return @"";
    else if(section == 1)
        return @"更多信息";
    else
        return @"";
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 2)
    {
        ChangeInfoViewController *changeController = [[ChangeInfoViewController alloc] init];
        changeController.viewType = kViewEdit;
        [[(UIViewController*)[[[self superview] superview] nextResponder] navigationController] pushViewController:changeController animated:YES];
        [changeController release];
        
    }
    
}

@end
